package com.hw.one.dam2.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hw.one.dam2.param.RegionSiteParam;
import com.hw.one.dam2.po.FengZhuang001PO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.DataCalculateTypeEnum;
import com.hw.one.dam2.common.DataTypeEnum;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.dao.ISiteAirDao;
import com.hw.one.dam2.entity.AQIEntity;
import com.hw.one.dam2.entity.ContrastRatioEntity;
import com.hw.one.dam2.entity.IAQIEntity;
import com.hw.one.dam2.param.SiteAirParam;
import com.hw.one.dam2.po.SiteAirQualityPO;
import com.hw.one.dam2.util.AQIProcess;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.dam2.util.WeatherUtil;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.util.DateTimeUtil;

@Service(value = "gPoinAirService")
public class GPoinAirService implements IGPointAirService {

    @Autowired
    private IRegionSiteDao regionSiteDao;

    @Autowired
    private ISiteAirDao siteAirDao;


    // 数字保留两位小数
    DecimalFormat decimalformat_2_scale = new DecimalFormat("######0.00");
    DecimalFormat decimalformat_3_scale = new DecimalFormat("######0.000");

    @SuppressWarnings("rawtypes")
    @Override
    public List<AQIEntity> findAirSiteAQIByTime(Map<String, Object> paraMap) throws Exception {
        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = String.valueOf(paraMap.get("pointType"));
        // 数据类型：小时数据 hourData；日数据 dayData；5分钟数据 minuiteData
        String dataType = String.valueOf(paraMap.get("dataType"));
        // 数据计算类型：实时 realTimeData，累计 addUpTimeData
        String dataCalculateType = String.valueOf(paraMap.get("dataCalculateType"));

        // 当前时间
        Calendar calendar = Calendar.getInstance();
        // 开始时间
        String startTime = String.valueOf(paraMap.get("startTime"));
        // 结束时间
        String endTime = String.valueOf(paraMap.get("endTime"));

        // 点位编码列表
        List<Map<String, Object>> siteInfoList = null;
        List<String> pointCodeList = new ArrayList<String>();
        Map<String, String> siteParaMap = new HashMap<String, String>();
        @SuppressWarnings("unchecked")
        Map<String, Object> region = UserUtil.getRegion();
        String regionCode = (String) region.get("regionCode");

        // 城市编码
        String cityCode = null;

        /**
         * 根据当前登录用户区域，查询该区域的市级编码
         */
        List<Map<String, Object>> parentRegionList = regionSiteDao.findParentRegionInfoByRegionCode(regionCode);
        // 把登录用户所属省份、和市编码，读取出来
        for (Map<String, Object> parentRegionMap : parentRegionList) {
            String retionType = String.valueOf(parentRegionMap.get("regionType"));
            if ("20".equals(retionType)) {
                cityCode = String.valueOf(parentRegionMap.get("regionCode"));
                break;
            }
        }
        if (null == cityCode) {
            cityCode = regionCode;
        }
//        siteParaMap.put("regionCode", cityCode);
//        siteParaMap.put("siteType", pointType);
        RegionSiteParam siteParam = new RegionSiteParam();
        siteParam.setRegionCode(cityCode);
        siteParam.setSiteType(pointType);
        siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);
        if (siteInfoList == null || siteInfoList.size() == 0) {
            return null;
        }
        // 传入的pointCodes 不为空
        if (!StringUtils.isEmpty(paraMap.get("pointCodes"))) {
            String pointCodes = (String) paraMap.get("pointCodes");
            String[] pointCodeS = pointCodes.split(",");
            List<String> siteCodeList = Arrays.asList(pointCodeS);
            List<Map<String, Object>> searchSiteInfoList = new ArrayList<>();
            for (Map<String, Object> siteMap : siteInfoList) {
                if (siteCodeList.contains(String.valueOf(siteMap.get("siteCode")))) {
                    searchSiteInfoList.add(siteMap);
                }
            }
            siteInfoList = searchSiteInfoList;
        }
        for (Map<String, Object> siteMap : siteInfoList) {
            pointCodeList.add(String.valueOf(siteMap.get("siteCode")));
        }

        // 查询参数 PO类
        SiteAirParam siteAirParam = new SiteAirParam();
        // 点位编码 列表
        siteAirParam.setSiteCodes(pointCodeList);

        List<AQIEntity> aqiList = new ArrayList<AQIEntity>();
        // 实时数据查询
        if (DataCalculateTypeEnum.REAL_TIME_DATA.getValue().equals(dataCalculateType)) {
            // 小时数据查询
            if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
                // 开始时间
                siteAirParam.setStartTime(startTime);
                // 结束时间
                siteAirParam.setEndTime(endTime);

                List<SiteAirQualityPO> hourAQIMapList = siteAirDao.findSiteAirHourDataBySiteCodesAndTime(siteAirParam);

                AQIEntity aqiEntity = null;
                // 把小时国控数据，转换为AQIEntity类
                int index = 1;
                for (SiteAirQualityPO hourAirMap : hourAQIMapList) {
                    aqiEntity = new AQIEntity();
                    String AQIObj = hourAirMap.getAQI();
                    aqiEntity.setIndex(index);
                    index++;
                    if (null != AQIObj && Integer.valueOf(AQIObj) > 0) {
                        aqiEntity.setAqi(AQIObj);
                    }
                    String AQILEVELObj = hourAirMap.getAQILEVEL();
                    if (null != AQILEVELObj && Integer.valueOf(AQILEVELObj) > 0) {
                        aqiEntity.setAqiLevel(AQILEVELObj);
                    }
                    String AQILEVELTYPEObj = hourAirMap.getAQILEVELTYPE();
                    if (null != AQILEVELTYPEObj) {
                        aqiEntity.setAqiLevelType(AQILEVELTYPEObj);
                    }
                    String POLLUTANTObj = hourAirMap.getPOLLUTANT();
                    if (null != POLLUTANTObj) {
                        aqiEntity.setPollutant(POLLUTANTObj);
                    }
                    String COMPOSITEVALUEObj = hourAirMap.getCOMPOSITEVALUE();
                    if (null != COMPOSITEVALUEObj && Double.valueOf(COMPOSITEVALUEObj) > 0) {
                        aqiEntity.setCompositeValue(decimalformat_2_scale.format(Double.parseDouble(COMPOSITEVALUEObj)));
                    }

                    String PM10Obj = hourAirMap.getPM10().getCONCENTRATION();
                    if (null != PM10Obj) {
                        aqiEntity.setPm10(PM10Obj);
                    }
                    String PM25Obj = hourAirMap.getPM25().getCONCENTRATION();
                    if (null != PM25Obj) {
                        aqiEntity.setPm25(PM25Obj);
                    }
                    String SO2Obj = hourAirMap.getSO2().getCONCENTRATION();
                    if (null != SO2Obj) {
                        aqiEntity.setSo2(SO2Obj);
                    }
                    String NO2Obj = hourAirMap.getNO2().getCONCENTRATION();
                    if (null != NO2Obj) {
                        aqiEntity.setNo2(NO2Obj);
                    }
                    String COObj = hourAirMap.getCO().getCONCENTRATION();
                    if (null != COObj) {
                        aqiEntity.setCo(decimalformat_3_scale.format(Double.parseDouble(COObj)));
                    }
                    String O3Obj = hourAirMap.getO3().getCONCENTRATION();
                    if (null != O3Obj) {
                        aqiEntity.setO3(O3Obj);
                    }

                    String SITECODEObj = hourAirMap.getSITECODE();
                    if (null != SITECODEObj) {
                        aqiEntity.setSiteCode(SITECODEObj);
                    }
                    String MONITORTIMEObj = hourAirMap.getMONITORTIME();
                    if (null != MONITORTIMEObj) {
                        aqiEntity.setMonitorTime(MONITORTIMEObj.substring(0, 16));
                    }
                    String SITENAMEObj = hourAirMap.getSITENAME();
                    if (null != SITENAMEObj) {
                        aqiEntity.setSiteName(SITENAMEObj);
                    }


                    aqiList.add(aqiEntity);
                }
                // 日数据查询
            } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {

                // 当前时间
                endTime = null == endTime || "".equals(endTime)
                    ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_H) : endTime;

                calendar.add(Calendar.DAY_OF_MONTH, -1);
                startTime = null == startTime || "".equals(startTime)
                    ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_H) : startTime;

                // 开始时间
                siteAirParam.setStartTime(startTime);
                // 结束时间
                siteAirParam.setEndTime(endTime);

                List<SiteAirQualityPO> dayAirMapList = siteAirDao.findSiteAirDayDataBySiteCodesAndTime(siteAirParam);
                AQIEntity aqiEntity = null;
                // 把日国控数据，转换为AQIEntity类
                int index = 1;
                for (SiteAirQualityPO hourAirMap : dayAirMapList) {
                    aqiEntity = new AQIEntity();
                    String AQIObj = hourAirMap.getAQI();
                    aqiEntity.setIndex(index);
                    index++;
                    if (null != AQIObj && Integer.valueOf(AQIObj) > 0) {
                        aqiEntity.setAqi(AQIObj);
                    }
                    String AQILEVELObj = hourAirMap.getAQILEVEL();
                    if (null != AQILEVELObj && Integer.valueOf(AQILEVELObj) > 0) {
                        aqiEntity.setAqiLevel(AQILEVELObj);
                    }
                    String AQILEVELTYPEObj = hourAirMap.getAQILEVELTYPE();
                    if (null != AQILEVELTYPEObj) {
                        aqiEntity.setAqiLevelType(AQILEVELTYPEObj);
                    }
                    String POLLUTANTObj = hourAirMap.getPOLLUTANT();
                    if (null != POLLUTANTObj) {
                        aqiEntity.setPollutant(POLLUTANTObj);
                    }
                    String COMPOSITEVALUEObj = hourAirMap.getCOMPOSITEVALUE();
                    if (null != COMPOSITEVALUEObj && Double.valueOf(COMPOSITEVALUEObj) > 0) {
                        aqiEntity.setCompositeValue(decimalformat_2_scale.format(Double.parseDouble(COMPOSITEVALUEObj)));
                    }

                    String PM10Obj = hourAirMap.getPM10().getCONCENTRATION();
                    if (null != PM10Obj) {
                        aqiEntity.setPm10(PM10Obj);
                    }
                    String PM25Obj = hourAirMap.getPM25().getCONCENTRATION();
                    if (null != PM25Obj) {
                        aqiEntity.setPm25(PM25Obj);
                    }
                    String SO2Obj = hourAirMap.getSO2().getCONCENTRATION();
                    if (null != SO2Obj) {
                        aqiEntity.setSo2(SO2Obj);
                    }
                    String NO2Obj = hourAirMap.getNO2().getCONCENTRATION();
                    if (null != NO2Obj) {
                        aqiEntity.setNo2(NO2Obj);
                    }
                    String COObj = hourAirMap.getCO().getCONCENTRATION();
                    if (null != COObj) {
                        aqiEntity.setCo(decimalformat_3_scale.format(Double.parseDouble(COObj)));
                    }
                    String O3Obj = hourAirMap.getO3().getCONCENTRATION();
                    if (null != O3Obj) {
                        aqiEntity.setO3(O3Obj);
                    }

                    String SITECODEObj = hourAirMap.getSITECODE();
                    if (null != SITECODEObj) {
                        aqiEntity.setSiteCode(SITECODEObj);
                    }
                    String MONITORTIMEObj = hourAirMap.getMONITORTIME();
                    if (null != MONITORTIMEObj) {
                        aqiEntity.setMonitorTime(MONITORTIMEObj.substring(0, 11));
                    }
                    String SITENAMEObj = hourAirMap.getSITENAME();
                    if (null != SITENAMEObj) {
                        aqiEntity.setSiteName(SITENAMEObj);
                    }
                    aqiList.add(aqiEntity);
                }

                // 分钟数据查询
            } else if (DataTypeEnum.MINUTE_DATA.getValue().equals(dataType)) {

                // 当前时间
                endTime = null == endTime || "".equals(endTime)
                    ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_HM) : endTime;

                calendar.add(Calendar.HOUR_OF_DAY, -1);
                startTime = null == startTime || "".equals(startTime)
                    ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_HM) : startTime;

                // 开始时间
                siteAirParam.setStartTime(startTime);
                // 结束时间
                siteAirParam.setEndTime(endTime);
                List<SiteAirQualityPO> minAirMapList = siteAirDao.findSiteAirMinDataBySiteCodesAndTime(siteAirParam);
                AQIEntity aqiEntity = null;
                // 把分钟国控数据，转换为AQIEntity类
                int index = 1;
                for (SiteAirQualityPO hourAirMap : minAirMapList) {
                    aqiEntity = new AQIEntity();
                    String AQIObj = hourAirMap.getAQI();
                    aqiEntity.setIndex(index);
                    index++;
                    if (null != AQIObj && Integer.valueOf(AQIObj) > 0) {
                        aqiEntity.setAqi(AQIObj);
                    }
                    String AQILEVELObj = hourAirMap.getAQILEVEL();
                    if (null != AQILEVELObj && Integer.valueOf(AQILEVELObj) > 0) {
                        aqiEntity.setAqiLevel(AQILEVELObj);
                    }
                    String AQILEVELTYPEObj = hourAirMap.getAQILEVELTYPE();
                    if (null != AQILEVELTYPEObj) {
                        aqiEntity.setAqiLevelType(AQILEVELTYPEObj);
                    }
                    String POLLUTANTObj = hourAirMap.getPOLLUTANT();
                    if (null != POLLUTANTObj) {
                        aqiEntity.setPollutant(POLLUTANTObj);
                    }
                    String COMPOSITEVALUEObj = hourAirMap.getCOMPOSITEVALUE();
                    if (null != COMPOSITEVALUEObj && Double.valueOf(COMPOSITEVALUEObj) > 0) {
                        aqiEntity.setCompositeValue(decimalformat_2_scale.format(Double.parseDouble(COMPOSITEVALUEObj)));
                    }

                    String PM10Obj = hourAirMap.getPM10().getCONCENTRATION();
                    if (null != PM10Obj) {
                        aqiEntity.setPm10(PM10Obj);
                    }
                    String PM25Obj = hourAirMap.getPM25().getCONCENTRATION();
                    if (null != PM25Obj) {
                        aqiEntity.setPm25(PM25Obj);
                    }
                    String SO2Obj = hourAirMap.getSO2().getCONCENTRATION();
                    if (null != SO2Obj) {
                        aqiEntity.setSo2(SO2Obj);
                    }
                    String NO2Obj = hourAirMap.getNO2().getCONCENTRATION();
                    if (null != NO2Obj) {
                        aqiEntity.setNo2(NO2Obj);
                    }
                    String COObj = hourAirMap.getCO().getCONCENTRATION();
                    if (null != COObj) {
                        aqiEntity.setCo(decimalformat_3_scale.format(Double.parseDouble(COObj)));
                    }
                    String O3Obj = hourAirMap.getO3().getCONCENTRATION();
                    if (null != O3Obj) {
                        aqiEntity.setO3(O3Obj);
                    }

                    String SITECODEObj = hourAirMap.getSITECODE();
                    if (null != SITECODEObj) {
                        aqiEntity.setSiteCode(SITECODEObj);
                    }
                    String MONITORTIMEObj = hourAirMap.getMONITORTIME();
                    if (null != MONITORTIMEObj) {
                        aqiEntity.setMonitorTime(MONITORTIMEObj.substring(0, 16));
                    }
                    String SITENAMEObj = hourAirMap.getSITENAME();
                    if (null != SITENAMEObj) {
                        aqiEntity.setSiteName(SITENAMEObj);
                    }
                    aqiList.add(aqiEntity);
                }

            }
            // 累计数据查询
        } else if (DataCalculateTypeEnum.ADD_UP_TIME_DATA.getValue().equals(dataCalculateType)) {
            // 把时间恢复成当前日期
            calendar = Calendar.getInstance();
            siteAirParam.setStartTime(DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD));
            siteAirParam.setEndTime(DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_H));
            List<SiteAirQualityPO> hourMapList = siteAirDao.findSiteAirMinDataBySiteCodesAndTime(siteAirParam);
            AQIEntity aqiEntity = null;

            // 带有排序的点位编码列表
            List<String> siteCodeSortedList = new ArrayList<String>();

            // 把小时国控数据，转换为AQIEntity类
            int index = 1;
            for (SiteAirQualityPO hourAirMap : hourMapList) {
                aqiEntity = new AQIEntity();
                String AQIObj = hourAirMap.getAQI();
                aqiEntity.setIndex(index);
                index++;
                if (null != AQIObj && Integer.valueOf(AQIObj) > 0) {
                    aqiEntity.setAqi(AQIObj);
                }
                String AQILEVELObj = hourAirMap.getAQILEVEL();
                if (null != AQILEVELObj && Integer.valueOf(AQILEVELObj) > 0) {
                    aqiEntity.setAqiLevel(AQILEVELObj);
                }
                String AQILEVELTYPEObj = hourAirMap.getAQILEVELTYPE();
                if (null != AQILEVELTYPEObj) {
                    aqiEntity.setAqiLevelType(AQILEVELTYPEObj);
                }
                String POLLUTANTObj = hourAirMap.getPOLLUTANT();
                if (null != POLLUTANTObj) {
                    aqiEntity.setPollutant(POLLUTANTObj);
                }
                String COMPOSITEVALUEObj = hourAirMap.getCOMPOSITEVALUE();
                if (null != COMPOSITEVALUEObj && Double.valueOf(COMPOSITEVALUEObj) > 0) {
                    aqiEntity.setCompositeValue(decimalformat_2_scale.format(Double.parseDouble(COMPOSITEVALUEObj)));
                }

                String PM10Obj = hourAirMap.getPM10().getCONCENTRATION();
                if (null != PM10Obj) {
                    aqiEntity.setPm10(PM10Obj);
                }
                String PM25Obj = hourAirMap.getPM25().getCONCENTRATION();
                if (null != PM25Obj) {
                    aqiEntity.setPm25(PM25Obj);
                }
                String SO2Obj = hourAirMap.getSO2().getCONCENTRATION();
                if (null != SO2Obj) {
                    aqiEntity.setSo2(SO2Obj);
                }
                String NO2Obj = hourAirMap.getNO2().getCONCENTRATION();
                if (null != NO2Obj) {
                    aqiEntity.setNo2(NO2Obj);
                }
                String COObj = hourAirMap.getCO().getCONCENTRATION();
                if (null != COObj) {
                    aqiEntity.setCo(decimalformat_3_scale.format(Double.parseDouble(COObj)));
                }
                String O3Obj = hourAirMap.getO3().getCONCENTRATION();
                if (null != O3Obj) {
                    aqiEntity.setO3(O3Obj);
                }

                String SITECODEObj = hourAirMap.getSITECODE();
                if (null != SITECODEObj) {
                    aqiEntity.setSiteCode(SITECODEObj);

                    // 把将要累计的点位编码提取出来
                    boolean isHave = false;
                    // 把点位编码按照已有顺序提取出来
                    for (String tempSiteCode : siteCodeSortedList) {
                        if (SITECODEObj.equals(tempSiteCode)) {
                            isHave = true;
                            break;
                        }
                    }

                    if (!isHave) {
                        siteCodeSortedList.add(SITECODEObj);
                    }

                }
                String MONITORTIMEObj = hourAirMap.getMONITORTIME();
                if (null != MONITORTIMEObj) {
                    aqiEntity.setMonitorTime(MONITORTIMEObj);
                }
                String SITENAMEObj = hourAirMap.getSITENAME();
                if (null != SITENAMEObj) {
                    aqiEntity.setSiteName(SITENAMEObj);
                }

                aqiList.add(aqiEntity);
            }

            List<AQIEntity> tempList = new ArrayList<AQIEntity>();

            AQIEntity tempAQI = null;
            int index1 = 1;
            for (String tempSiteCode : siteCodeSortedList) {

                int pm10 = 0;
                int pm25 = 0;
                int so2 = 0;
                int no2 = 0;
                double co = 0;
                int o3 = 0;

                int sum = 0;

                String monitorTime = null;
                String siteName = null;

                for (AQIEntity hourPO : aqiList) {
                    String siteCode = hourPO.getSiteCode();
                    monitorTime = hourPO.getMonitorTime().substring(0, 10);

                    if (tempSiteCode.equals(siteCode)) {
                        siteName = hourPO.getSiteName();
                        sum = sum + 1;
                        pm10 = pm10 + Integer.valueOf(hourPO.getPm10());
                        pm25 = pm25 + Integer.valueOf(hourPO.getPm25());
                        so2 = so2 + Integer.valueOf(hourPO.getSo2());
                        no2 = no2 + Integer.valueOf(hourPO.getNo2());
                        co = co + Double.valueOf(hourPO.getCo());
                        o3 = o3 + Integer.valueOf(hourPO.getO3());
                    }
                }

                tempAQI = new AQIEntity();
                tempAQI.setSiteCode(tempSiteCode);
                tempAQI.setSiteName(siteName);
                tempAQI.setMonitorTime(monitorTime);
                tempAQI.setIndex(index1);
                index1++;
                if (sum > 0) {

                    pm10 = Math.round(pm10 / sum);
                    pm25 = Math.round(pm25 / sum);
                    so2 = Math.round(so2 / sum);
                    no2 = Math.round(no2 / sum);
                    co = co / sum;
                    o3 = Math.round(o3 / sum);

                    tempAQI.setPm10(String.valueOf(pm10));
                    tempAQI.setPm25(String.valueOf(pm25));
                    tempAQI.setSo2(String.valueOf(so2));
                    tempAQI.setNo2(String.valueOf(no2));
                    tempAQI.setCo(decimalformat_3_scale.format(co));
                    tempAQI.setO3(String.valueOf(o3));

                    int aqi = AQIProcess.getAqi(pm10, pm25, so2, no2, co, o3);
                    tempAQI.setAqi(String.valueOf(aqi));

                    int aqiLevel = AQIProcess.getAqiLevel(aqi);
                    tempAQI.setAqiLevel(String.valueOf(aqiLevel));

                    String aqiLevelType = AQIProcess.getAqiLevelType(aqiLevel);
                    tempAQI.setAqiLevelType(aqiLevelType);

                    String comprehensiveIndex = AQIProcess.getComprehensiveIndex(pm10, pm25, so2, no2, co, o3);
                    tempAQI.setCompositeValue(comprehensiveIndex);

                    String pollution = AQIProcess.getPollution(pm10, pm25, so2, no2, co, o3);
                    tempAQI.setPollutant(pollution);

                }
                tempList.add(tempAQI);
            }

            aqiList.clear();
            aqiList.addAll(tempList);

        }

        return aqiList;
    }

    @Override
    public List<IAQIEntity> findGPointWeekIAQIBySiteCodeAndTime(Map<String, Object> paraMap) throws Exception {
        // 年份
        String year = paraMap.get("year").toString();
        // 第几周
        String weekNum = paraMap.get("weekNum").toString();

        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = paraMap.get("pointType").toString();

        Map<String, Object> region = UserUtil.getRegion();
        String regionCode = (String) region.get("regionCode");
        // String regionType = (String) region.get("regionType");
        String cityCode = null;
        /**
         * 根据当前登录用户区域，查询该区域的上级区域
         */
        List<Map<String, Object>> parentRegionList = regionSiteDao.findParentRegionInfoByRegionCode(regionCode);
        // 把登录用户所属省份、和市编码，读取出来
        for (Map<String, Object> parentRegionMap : parentRegionList) {
            String retionType = String.valueOf(parentRegionMap.get("regionType"));
            // 市级名称
            if ("20".equals(retionType)) {
                cityCode = String.valueOf(parentRegionMap.get("regionCode"));
                break;
            }
        }

        if (null == cityCode) {
            cityCode = regionCode;
        }

        // 点位编码列表
        List<Map<String, Object>> siteInfoList = null;
        List<String> pointCodeList = new ArrayList<String>();
//        Map<String, String> siteMap = new HashMap<String, String>();
//        siteMap.put("regionCode", cityCode);
//        siteMap.put("siteType", pointType);
        RegionSiteParam siteParam = new RegionSiteParam();
        siteParam.setRegionCode(cityCode);
        siteParam.setSiteType(pointType);

        siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);
        if (siteInfoList == null || siteInfoList.size() == 0) {
            return null;
        }
        // 传入的pointCodes 不为空
        if (!StringUtils.isEmpty(paraMap.get("pointCodes"))) {
            String pointCodes = (String) paraMap.get("pointCodes");
            String[] pointCodeS = pointCodes.split(",");
            List<String> siteCodeList = Arrays.asList(pointCodeS);
            List<Map<String, Object>> searchSiteInfoList = new ArrayList<>();
            for (Map<String, Object> siteInfo : siteInfoList) {
                if (siteCodeList.contains(String.valueOf(siteInfo.get("siteCode")))) {
                    searchSiteInfoList.add(siteInfo);
                }
            }
            siteInfoList = searchSiteInfoList;
        }
        for (Map<String, Object> siteInfoMap : siteInfoList) {
            pointCodeList.add(String.valueOf(siteInfoMap.get("siteCode")));
        }
        // 查询条件
        SiteAirParam siteAirParam = new SiteAirParam();
        siteAirParam.setYear(year);
        siteAirParam.setWeek(weekNum);
        siteAirParam.setSiteCodes(pointCodeList);

        List<SiteAirQualityPO> airWeekReportList = siteAirDao.findSiteAirWeekDataBySiteCodesAndTime(siteAirParam);
//        // 把点位名称和数据关联起来
        List<IAQIEntity> iaqiList = new ArrayList<IAQIEntity>();
        IAQIEntity iaqiPO = null;
        int index = 1;
        for (SiteAirQualityPO weekMap : airWeekReportList) {
            iaqiPO = new IAQIEntity();
            iaqiPO.setIndex(index);
            index++;
            iaqiPO.setSiteName(weekMap.getSITENAME());
            iaqiPO.setCompositeValue(weekMap.getCOMPOSITEVALUE());
            iaqiPO.setAqi(weekMap.getAQI());
            iaqiPO.setPm10(weekMap.getPM10().getCONCENTRATION());
            iaqiPO.setPm10IAQI(weekMap.getPM10().getSUBINDEX());
            iaqiPO.setPm25(weekMap.getPM25().getCONCENTRATION());
            iaqiPO.setPm25IAQI(weekMap.getPM25().getSUBINDEX());
            iaqiPO.setSo2(weekMap.getSO2().getCONCENTRATION());
            iaqiPO.setSo2IAQI(weekMap.getSO2().getSUBINDEX());
            iaqiPO.setNo2(weekMap.getNO2().getCONCENTRATION());
            iaqiPO.setNo2IAQI(weekMap.getNO2().getSUBINDEX());
            iaqiPO.setCo(weekMap.getCO().getCONCENTRATION());
            iaqiPO.setCoIAQI(weekMap.getCO().getSUBINDEX());
            iaqiPO.setO3(weekMap.getO3().getCONCENTRATION());
            iaqiPO.setO3IAQI(weekMap.getO3().getSUBINDEX());

            iaqiList.add(iaqiPO);
        }

        return iaqiList;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public List<ContrastRatioEntity> findGPointContrastRatioBySiteCodeAndTime(Map<String, Object> paraMap)
        throws Exception {
        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = paraMap.get("pointType").toString();
        // 数据类型 日报dayData;周报weekData;月报monthData;年报yearData
        String dataType = paraMap.get("dataType").toString();

        // 当前时间
        Calendar calendar = Calendar.getInstance();
        // 当前实际年份
        int actualYear = calendar.get(Calendar.YEAR);
        String actualYearStr = String.valueOf(actualYear);
        // 实际月份
        int actualMonth = calendar.get(Calendar.MONTH) + 1;
        String actualMonthStr = String.valueOf(actualMonth);

        // 年份
        String yearNumStr = paraMap.get("yearNum").toString();
        yearNumStr = null == yearNumStr || "".equals(yearNumStr) ? actualYearStr : yearNumStr;

        // 月份或者周数
        String monthOrWeekNumStr = paraMap.get("monthOrWeekNum").toString();
        monthOrWeekNumStr = null == monthOrWeekNumStr || "".equals(monthOrWeekNumStr)
            ? String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR) - 1) : monthOrWeekNumStr;

        Map<String, Object> region = UserUtil.getRegion();
        String regionCode = (String) region.get("regionCode");
        String cityCode = null;
        /**
         * 根据当前登录用户区域，查询该区域的上级区域
         */
        List<Map<String, Object>> parentRegionList = regionSiteDao.findParentRegionInfoByRegionCode(regionCode);
        // 把登录用户所属省份、和市编码，读取出来
        for (Map<String, Object> parentRegionMap : parentRegionList) {
            String retionType = String.valueOf(parentRegionMap.get("regionType"));
            // 市级名称
            if ("20".equals(retionType)) {
                cityCode = String.valueOf(parentRegionMap.get("regionCode"));
                break;
            }
        }

        if (null == cityCode) {
            cityCode = regionCode;
        }

        // 点位编码列表
        List<Map<String, Object>> siteInfoList = null;
        List<String> pointCodeList = new ArrayList<String>();

        // 返回的list
        List<ContrastRatioEntity> cvList = new ArrayList<ContrastRatioEntity>();
        // 拼凑查询条件
//        Map<String, String> siteMap = new HashMap<String, String>();
//        siteMap.put("regionCode", cityCode);
//        siteMap.put("siteType", pointType);
        RegionSiteParam siteParam = new RegionSiteParam();
        siteParam.setRegionCode(cityCode);
        siteParam.setSiteType(pointType);
        // 读取站点信息
        siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);
        if (siteInfoList == null || siteInfoList.size() == 0) {
            return null;
        }
        // 传入的pointCodes 不为空
        if (!StringUtils.isEmpty(paraMap.get("pointCodes"))) {
            String pointCodes = (String) paraMap.get("pointCodes");
            String[] pointCodeS = pointCodes.split(",");
            List<String> siteCodeList = Arrays.asList(pointCodeS);
            List<Map<String, Object>> searchSiteInfoList = new ArrayList<>();
            for (Map<String, Object> siteInfo : siteInfoList) {
                if (siteCodeList.contains(String.valueOf(siteInfo.get("siteCode")))) {
                    searchSiteInfoList.add(siteInfo);
                }
            }
            siteInfoList = searchSiteInfoList;
        }
        // 提取点位编码
        for (Map<String, Object> siteInfoMap : siteInfoList) {
            pointCodeList.add(String.valueOf(siteInfoMap.get("siteCode")));
        }

        // 查询 Mongo的条件PO
        SiteAirParam siteAirParam = new SiteAirParam();
        // 设置 点位编码
        siteAirParam.setSiteCodes(pointCodeList);
        // 日报 按日计算 同比、环比
        if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
            // 开始时间 2017-07-08 12:12:12
            String startTime = paraMap.get("startTime").toString();
            // 结束时间 2017-07-10 12:12:12
            String endTime = paraMap.get("endTime").toString();

            siteAirParam.setStartTime(startTime);
            siteAirParam.setEndTime(endTime);

            // 当前日数据
            List<SiteAirQualityPO> curDayList = siteAirDao.findSiteAirDayDataBySiteCodesAndTime(siteAirParam);


            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            int days = (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 3600 * 24));
            // 向前推days数据
            calendar.setTime(startDate);
            calendar.add(Calendar.DAY_OF_MONTH, -days);

            siteAirParam.setStartTime(sdf.format(calendar.getTime()));
            siteAirParam.setEndTime(startTime);

            // 前days天 日数据
            List<SiteAirQualityPO> lastDayList = siteAirDao.findSiteAirDayDataBySiteCodesAndTime(siteAirParam);

            // 上一年相应日期数据
            calendar.setTime(startDate);
            calendar.add(Calendar.YEAR, -1);
            String lastYearStartTime = sdf.format(calendar.getTime());
            calendar.setTime(endDate);
            calendar.add(Calendar.YEAR, -1);
            String lastYearEndTime = sdf.format(calendar.getTime());

            siteAirParam.setStartTime(lastYearStartTime);
            siteAirParam.setEndTime(lastYearEndTime);

            // 上一年 日数据
            List<SiteAirQualityPO> lastYearList = siteAirDao.findSiteAirDayDataBySiteCodesAndTime(siteAirParam);

            List<SiteAirQualityPO> curDayAverageList = getSiteAirQualityAverageList(curDayList, pointCodeList);
            List<SiteAirQualityPO> lastDayAverageList = getSiteAirQualityAverageList(lastDayList, pointCodeList);
            List<SiteAirQualityPO> lastYearAverageList = getSiteAirQualityAverageList(lastYearList, pointCodeList);

            /**
             * 计算 空气质量 环比, 和同比
             */
            List<Map<String, Object>> resultHBList = setAirQualityHBAndTBMethod(curDayAverageList, lastDayAverageList, lastYearAverageList);

            setSiteNameMetiod(siteInfoList, resultHBList);

            ContrastRatioEntity crentity = null;
            for (Map curDay : resultHBList) {
                crentity = new ContrastRatioEntity();
                crentity.setIndex(Integer.valueOf(curDay.get("index").toString()));
                crentity.setSiteName(curDay.get("siteName").toString());
                crentity.setCompositeValue(curDay.get("COMPOSITEVALUE").toString());
                crentity.setCompositeTBRatio(curDay.get("cvTB"));
                crentity.setCompositeHBRatio(curDay.get("cvHB"));
                crentity.setPm10(curDay.get("PM10").toString());
                crentity.setPm10TBRatio(curDay.get("pm10TB"));
                crentity.setPm10HBRatio(curDay.get("pm10HB"));
                crentity.setPm25(curDay.get("PM25").toString());
                crentity.setPm25TBRatio(curDay.get("pm25TB"));
                crentity.setPm25HBRatio(curDay.get("pm25HB"));
                crentity.setSo2(curDay.get("SO2").toString());
                crentity.setSo2TBRatio(curDay.get("so2TB"));
                crentity.setSo2HBRatio(curDay.get("so2HB"));
                crentity.setNo2(curDay.get("NO2").toString());
                crentity.setNo2TBRatio(curDay.get("no2TB"));
                crentity.setNo2HBRatio(curDay.get("no2HB"));
                crentity.setCo(curDay.get("CO").toString());
                crentity.setCoTBRatio(curDay.get("coTB"));
                crentity.setCoHBRatio(curDay.get("coHB"));
                crentity.setO3(curDay.get("O3H8").toString());
                crentity.setO3TBRatio(curDay.get("o3TB"));
                crentity.setO3HBRatio(curDay.get("o3HB"));

                cvList.add(crentity);
            }

            return cvList;
            // 周报 按周计算 同比、环比
        } else if (dataType.equals(DataTypeEnum.WEEK_DATA.getValue())) {
            siteAirParam.setYear(yearNumStr);
            siteAirParam.setWeek(monthOrWeekNumStr);

            // 当前周数据
            List<SiteAirQualityPO> curWeekList = siteAirDao.findSiteAirWeekDataBySiteCodesAndTime(siteAirParam);

            int weekNum = Integer.parseInt(monthOrWeekNumStr);
            // 周数减一
            monthOrWeekNumStr = String.valueOf(weekNum - 1);
            siteAirParam.setWeek(monthOrWeekNumStr);
            // 当前周的上一周数据
            List<SiteAirQualityPO> lastWeekList = siteAirDao.findSiteAirWeekDataBySiteCodesAndTime(siteAirParam);


            // 上一年的当前周数据，计算同比
            int yearNum = Integer.parseInt(yearNumStr);
            yearNumStr = String.valueOf(yearNum - 1);
            // 年份，上一年
            siteAirParam.setYear(yearNumStr);
            // 周数，还是原来的周数，而不是经过减一的monthOrWeekNumStr。
            siteAirParam.setWeek(String.valueOf(weekNum));
            List<SiteAirQualityPO> lastYearList = siteAirDao.findSiteAirWeekDataBySiteCodesAndTime(siteAirParam);

            /**
             * 计算 空气质量 环比, 和同比
             */
            List<Map<String, Object>> resultHBList = setAirQualityHBAndTBMethod(curWeekList, lastWeekList, lastYearList);

            /**
             * 设置站点名称
             */
            setSiteNameMetiod(siteInfoList, resultHBList);

            ContrastRatioEntity crentity = null;
            for (Map weekMap : resultHBList) {
                crentity = new ContrastRatioEntity();
                crentity.setIndex(Integer.valueOf(String.valueOf(weekMap.get("index"))));
                crentity.setSiteName(String.valueOf(weekMap.get("siteName")));
                crentity.setCompositeValue(weekMap.get("COMPOSITEVALUE").toString());
                crentity.setCompositeTBRatio(weekMap.get("cvTB"));
                crentity.setCompositeHBRatio(weekMap.get("cvHB"));
                crentity.setPm10(weekMap.get("PM10").toString());
                crentity.setPm10TBRatio(weekMap.get("pm10TB"));
                crentity.setPm10HBRatio(weekMap.get("pm10HB"));
                crentity.setPm25(weekMap.get("PM25").toString());
                crentity.setPm25TBRatio(weekMap.get("pm25TB"));
                crentity.setPm25HBRatio(weekMap.get("pm25HB"));
                crentity.setSo2(weekMap.get("SO2").toString());
                crentity.setSo2TBRatio(weekMap.get("so2TB"));
                crentity.setSo2HBRatio(weekMap.get("so2HB"));
                crentity.setNo2(weekMap.get("NO2").toString());
                crentity.setNo2TBRatio(weekMap.get("no2TB"));
                crentity.setNo2HBRatio(weekMap.get("no2HB"));
                crentity.setCo(weekMap.get("CO").toString());
                crentity.setCoTBRatio(weekMap.get("coTB"));
                crentity.setCoHBRatio(weekMap.get("coHB"));
                crentity.setO3(weekMap.get("O3H8").toString());
                crentity.setO3TBRatio(weekMap.get("o3TB"));
                crentity.setO3HBRatio(weekMap.get("o3HB"));

                cvList.add(crentity);
            }

            return cvList;
            // 月报 按月计算 同比、环比
        } else if (dataType.equals(DataTypeEnum.MONTH_DATA.getValue())) {

            monthOrWeekNumStr = Integer.valueOf(monthOrWeekNumStr) > 9 ? monthOrWeekNumStr : "0" + monthOrWeekNumStr;
            String searchTime = CalendarUtil.getLastDayOfMonth(yearNumStr + "-" + monthOrWeekNumStr) + " 00:00:00";
            siteAirParam.setStartTime(searchTime);
            siteAirParam.setEndTime(searchTime);

            // 当前月数据
            List<SiteAirQualityPO> curMonthList = siteAirDao.findSiteAirMonthDataBySiteCodesAndTime(siteAirParam);

            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(CalendarUtil.getDateByString(searchTime, DamConstant.ALL_SDF_YMD));
            calendar1.add(Calendar.YEAR, -1);
            String lastYearTime = CalendarUtil.getDateFormat(calendar1.getTime(), DamConstant.ALL_SDF_YMD);
            siteAirParam.setStartTime(lastYearTime);
            siteAirParam.setEndTime(lastYearTime);

            // 上一年 本月数据
            List<SiteAirQualityPO> lastYearMonthList = siteAirDao.findSiteAirMonthDataBySiteCodesAndTime(siteAirParam);


            calendar1.setTime(CalendarUtil.getDateByString(searchTime, DamConstant.ALL_SDF_YMD));
            calendar1.add(Calendar.MONTH, -1);
            String lastMonthTime = CalendarUtil.getDateFormat(calendar1.getTime(), DamConstant.ALL_SDF_YMD);
            siteAirParam.setStartTime(lastMonthTime);
            siteAirParam.setEndTime(lastMonthTime);
            //
            List<SiteAirQualityPO> lastMonthList = siteAirDao.findSiteAirMonthDataBySiteCodesAndTime(siteAirParam);

            /**
             * 计算 同比和环比
             */
            List<Map<String, Object>> resultTBList = setAirQualityTBAndHBMethodByMonth(curMonthList, lastYearMonthList, lastMonthList);

            setSiteNameMetiod(siteInfoList, resultTBList);
            ContrastRatioEntity crentity = null;
            for (Map curMonth : resultTBList) {
                crentity = new ContrastRatioEntity();
                crentity.setIndex(Integer.valueOf(curMonth.get("index").toString()));
                crentity.setSiteName(curMonth.get("siteName").toString());
                crentity.setCompositeValue(curMonth.get("COMPOSITEVALUE").toString());
                crentity.setCompositeTBRatio(curMonth.get("cvTB"));
                crentity.setCompositeHBRatio(curMonth.get("cvHB"));
                crentity.setPm10(curMonth.get("PM10").toString());
                crentity.setPm10TBRatio(curMonth.get("pm10TB"));
                crentity.setPm10HBRatio(curMonth.get("pm10HB"));
                crentity.setPm25(curMonth.get("PM25").toString());
                crentity.setPm25TBRatio(curMonth.get("pm25TB"));
                crentity.setPm25HBRatio(curMonth.get("pm25HB"));
                crentity.setSo2(curMonth.get("SO2").toString());
                crentity.setSo2TBRatio(curMonth.get("so2TB"));
                crentity.setSo2HBRatio(curMonth.get("so2HB"));
                crentity.setNo2(curMonth.get("NO2").toString());
                crentity.setNo2TBRatio(curMonth.get("no2TB"));
                crentity.setNo2HBRatio(curMonth.get("no2HB"));
                crentity.setCo(curMonth.get("CO").toString());
                crentity.setCoTBRatio(curMonth.get("coTB"));
                crentity.setCoHBRatio(curMonth.get("coHB"));
                crentity.setO3(curMonth.get("O3_8").toString());
                crentity.setO3TBRatio(curMonth.get("o3TB"));
                crentity.setO3HBRatio(curMonth.get("o3HB"));

                cvList.add(crentity);
            }
            return cvList;
            // 年报 按年计算 同比
        } else if (dataType.equals(DataTypeEnum.YEAR_DATA.getValue())) {
            String searchTime = yearNumStr + "-12-31 00:00:00";
            siteAirParam.setStartTime(searchTime);
            siteAirParam.setEndTime(searchTime);

            // 当前年数据
            List<SiteAirQualityPO> curYearList = siteAirDao.findSiteAirYearDataBySiteCodesAndTime(siteAirParam);

            calendar.setTime(CalendarUtil.getDateByString(searchTime, DamConstant.ALL_SDF_YMD));
            calendar.add(Calendar.YEAR, -1);
            String lastYearTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD);

            // 去年的数据
            siteAirParam.setStartTime(lastYearTime);
            siteAirParam.setEndTime(lastYearTime);

            List<SiteAirQualityPO> lastYearList = siteAirDao.findSiteAirYearDataBySiteCodesAndTime(siteAirParam);

            // 环比数据
            List<Map<String, Object>> resultHBList = setAirQualityHBMethodByMonth(curYearList, lastYearList);

            setSiteNameMetiod(siteInfoList, resultHBList);

            ContrastRatioEntity crentity = null;
            for (Map curMonth : resultHBList) {
                crentity = new ContrastRatioEntity();
                crentity.setIndex(Integer.valueOf(curMonth.get("index").toString()));
                crentity.setSiteName(curMonth.get("siteName").toString());
                crentity.setCompositeValue(curMonth.get("COMPOSITEVALUE").toString());
                crentity.setCompositeTBRatio(curMonth.get("cvTB"));
                crentity.setCompositeHBRatio(curMonth.get("cvHB"));
                crentity.setPm10(curMonth.get("PM10").toString());
                crentity.setPm10TBRatio(curMonth.get("pm10TB"));
                crentity.setPm10HBRatio(curMonth.get("pm10HB"));
                crentity.setPm25(curMonth.get("PM25").toString());
                crentity.setPm25TBRatio(curMonth.get("pm25TB"));
                crentity.setPm25HBRatio(curMonth.get("pm25HB"));
                crentity.setSo2(curMonth.get("SO2").toString());
                crentity.setSo2TBRatio(curMonth.get("so2TB"));
                crentity.setSo2HBRatio(curMonth.get("so2HB"));
                crentity.setNo2(curMonth.get("NO2").toString());
                crentity.setNo2TBRatio(curMonth.get("no2TB"));
                crentity.setNo2HBRatio(curMonth.get("no2HB"));
                crentity.setCo(curMonth.get("CO").toString());
                crentity.setCoTBRatio(curMonth.get("coTB"));
                crentity.setCoHBRatio(curMonth.get("coHB"));
                crentity.setO3(curMonth.get("O3_8").toString());
                crentity.setO3TBRatio(curMonth.get("o3TB"));
                crentity.setO3HBRatio(curMonth.get("o3HB"));

                cvList.add(crentity);
            }
            return cvList;
        }

        return null;
    }

    private List<SiteAirQualityPO> getSiteAirQualityAverageList(List<SiteAirQualityPO> originalList, List<String> siteCodeList) {
        List<SiteAirQualityPO> averageList = new ArrayList<>(siteCodeList.size());
        for (String siteCode : siteCodeList) {
            int pm10Num = 0;
            int pm25Num = 0;
            int so2Num = 0;
            int no2Num = 0;
            double coNum = 0;
            int o3Num = 0;
            int num = 0;

            String siteName = "";
            for (SiteAirQualityPO po : originalList) {
                if (siteCode.equals(po.getSITECODE())) {
                    siteName = po.getSITENAME();
                    String pm10 = po.getPM10().getCONCENTRATION();
                    if (null != pm10 && !"".equals(pm10) && !"-".equals(pm10.charAt(0)) && !"0".equals(pm10)) {
                        pm10Num += Integer.valueOf(pm10);
                    }
                    String pm25 = po.getPM25().getCONCENTRATION();
                    if (null != pm25 && !"".equals(pm25) && !"-".equals(pm25.charAt(0)) && !"0".equals(pm25)) {
                        pm25Num += Integer.valueOf(pm25);
                    }
                    String co = po.getCO().getCONCENTRATION();
                    if (null != co && !"".equals(co) && !"-".equals(co.charAt(0)) && !"0".equals(co)) {
                        coNum += Double.valueOf(co);
                    }
                    String so2 = po.getSO2().getCONCENTRATION();
                    if (null != so2 && !"".equals(so2) && !"-".equals(so2.charAt(0)) && !"0".equals(so2)) {
                        so2Num += Integer.valueOf(so2);
                    }
                    String no2 = po.getNO2().getCONCENTRATION();
                    if (null != no2 && !"".equals(no2) && !"-".equals(no2.charAt(0)) && !"0".equals(no2)) {
                        no2Num += Integer.valueOf(no2);
                    }
                    String o3 = po.getO3().getCONCENTRATION();
                    if (null != o3 && !"".equals(o3) && !"-".equals(o3.charAt(0)) && !"0".equals(o3)) {
                        o3Num += Integer.valueOf(o3);
                    }
                    num++;
                }
            }
            if (num > 0) {
                pm10Num = Math.round(pm10Num / num);
                pm25Num = Math.round(pm25Num / num);
                no2Num = Math.round(no2Num / num);
                so2Num = Math.round(so2Num / num);
                o3Num = Math.round(o3Num / num);
                coNum = coNum / num;

                SiteAirQualityPO newPo = new SiteAirQualityPO();
                newPo.setSITENAME(siteName);
                newPo.setSITECODE(siteCode);

                newPo.setPM10(new FengZhuang001PO());
                newPo.getPM10().setCONCENTRATION(pm10Num + "");

                newPo.setPM25(new FengZhuang001PO());
                newPo.getPM25().setCONCENTRATION(pm25Num + "");

                newPo.setNO2(new FengZhuang001PO());
                newPo.getNO2().setCONCENTRATION(no2Num + "");

                newPo.setSO2(new FengZhuang001PO());
                newPo.getSO2().setCONCENTRATION(so2Num + "");

                newPo.setO3(new FengZhuang001PO());
                newPo.getO3().setCONCENTRATION(o3Num + "");

                newPo.setCO(new FengZhuang001PO());
                newPo.getCO().setCONCENTRATION(decimalformat_2_scale.format(coNum));

                int aqi = AQIProcess.getAqi(pm10Num, pm25Num, so2Num, no2Num, coNum, o3Num);
                newPo.setAQI(String.valueOf(aqi));

                int aqiLevel = AQIProcess.getAqiLevel(aqi);
                newPo.setAQILEVEL(String.valueOf(aqiLevel));

                String aqiLevelType = AQIProcess.getAqiLevelType(aqiLevel);
                newPo.setAQILEVELTYPE(aqiLevelType);

                String comprehensiveIndex = AQIProcess.getComprehensiveIndex(pm10Num, pm25Num, so2Num, no2Num, coNum, o3Num);
                newPo.setCOMPOSITEVALUE(comprehensiveIndex);

                String pollution = AQIProcess.getPollution(pm10Num, pm25Num, so2Num, no2Num, coNum, o3Num);
                newPo.setPOLLUTANT(pollution);

                averageList.add(newPo);
            }

        }
        return averageList;
    }

    private List<Map<String, Object>> setAirQualityHBAndTBMethod(List<SiteAirQualityPO> curWeekList,
                                                                 List<SiteAirQualityPO> lastWeekList,
                                                                 List<SiteAirQualityPO> lastYearWeekList) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        // 计算周环比
        for (SiteAirQualityPO curWeekMap : curWeekList) {
            Map<String, Object> resultMap = new HashMap<>();
            // 点位编码
            String curSiteCode = curWeekMap.getSITECODE();
            String curAQI = curWeekMap.getAQI();
            String curCV = curWeekMap.getCOMPOSITEVALUE();
            String curPM10 = curWeekMap.getPM10().getCONCENTRATION();
            String curPM25 = curWeekMap.getPM25().getCONCENTRATION();
            String curSO2 = curWeekMap.getSO2().getCONCENTRATION();
            String curNO2 = curWeekMap.getNO2().getCONCENTRATION();
            String curCO = curWeekMap.getCO().getCONCENTRATION();
            String curO3 = curWeekMap.getO3().getCONCENTRATION();
            resultMap.put("SITECODE", curSiteCode);
            resultMap.put("COMPOSITEVALUE", curCV);
            resultMap.put("PM10", curPM10);
            resultMap.put("PM25", curPM25);
            resultMap.put("SO2", curSO2);
            resultMap.put("NO2", curNO2);
            resultMap.put("CO", curCO);
            resultMap.put("O3H8", curO3);
            // 上周的数据
            boolean hbFlag = true;
            for (SiteAirQualityPO lastWeekPO : lastWeekList) {
                String lastSiteCode = lastWeekPO.getSITECODE();
                if (lastSiteCode.equals(curSiteCode)) {
                    hbFlag = false;
                    String lastAQI = lastWeekPO.getAQI();
                    String lastCV = lastWeekPO.getCOMPOSITEVALUE();
                    String lastPM10 = lastWeekPO.getPM10().getCONCENTRATION();
                    String lastPM25 = lastWeekPO.getPM25().getCONCENTRATION();
                    String lastSO2 = lastWeekPO.getSO2().getCONCENTRATION();
                    String lastNO2 = lastWeekPO.getNO2().getCONCENTRATION();
                    String lastCO = lastWeekPO.getCO().getCONCENTRATION();
                    String lastO3 = lastWeekPO.getO3().getCONCENTRATION();
                    // 计算aqi的环比值
                    if (null == curAQI || null == lastAQI || "0".equals(lastAQI)) {

                        resultMap.put("aqiHB", "-");
                    }else {
                        double aqiRetio = (Double.valueOf(curAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI)
                                * 100;
                        resultMap.put("aqiHB", decimalformat_2_scale.format(aqiRetio));
                    }
                    // 计算综合指数的环比值
                    if (null == curCV || null == lastCV || "0.00".equals(lastCV) || "0".equals(lastCV)) {

                        resultMap.put("cvHB", "-");
                    }else {
                        double CVRetio = (Double.valueOf(curCV) - Double.valueOf(lastCV)) / Double.valueOf(lastCV)
                                * 100;
                        resultMap.put("cvHB", decimalformat_2_scale.format(CVRetio));
                    }
                    // pm10的环比值
                    if (null == curPM10 || null == lastPM10 || "0".equals(lastPM10)) {

                        resultMap.put("pm10HB", "-");
                    }else {
                        double PM10Retio = (Double.valueOf(curPM10) - Double.valueOf(lastPM10))
                                / Double.valueOf(lastPM10) * 100;
                        resultMap.put("pm10HB", decimalformat_2_scale.format(PM10Retio));
                    }
                    // pm25的环比值
                    if (null == curPM25 || null == lastPM25 || "0".equals(lastPM25)) {

                        resultMap.put("pm25HB", "-");
                    }else {
                        double PM25Retio = (Double.valueOf(curAQI) - Double.valueOf(lastPM25))
                                / Double.valueOf(lastPM25) * 100;
                        resultMap.put("pm25HB", decimalformat_2_scale.format(PM25Retio));
                    }
                    // SO2的环比值
                    if (null == curSO2 || null == lastSO2 || "0".equals(lastSO2)) {

                        resultMap.put("so2HB", "-");
                    }else {
                        double SO2Retio = (Double.valueOf(curSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2)
                                * 100;
                        resultMap.put("so2HB", decimalformat_2_scale.format(SO2Retio));
                    }
                    // NO2的环比值
                    if (null == curNO2 || null == lastNO2 || "0".equals(lastNO2)) {

                        resultMap.put("no2HB", "-");
                    }else {
                        double NO2Retio = (Double.valueOf(curNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2)
                                * 100;
                        resultMap.put("no2HB", decimalformat_2_scale.format(NO2Retio));
                    }
                    // CO的环比值
                    if (null == curCO || null == lastCO || "0.00".equals(lastCO) || "0".equals(lastCO)) {

                        resultMap.put("coHB", "-");
                    }else {
                        double CORetio = (Double.valueOf(curCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO)
                                * 100;
                        resultMap.put("coHB", decimalformat_2_scale.format(CORetio));
                    }
                    // O3的环比值
                    if (null == curO3 || null == lastO3 || "0".equals(lastO3)) {
                        resultMap.put("o3HB", "-");

                    }else {
                        double O3Retio = (Double.valueOf(curO3) - Double.valueOf(lastO3)) / Double.valueOf(lastO3)
                                * 100;
                        resultMap.put("o3HB", decimalformat_2_scale.format(O3Retio));
                    }
                    break;
                }
            }
            if (hbFlag) {
                resultMap.put("aqiHB", "-");
                resultMap.put("cvHB", "-");
                resultMap.put("pm10HB", "-");
                resultMap.put("pm25HB", "-");
                resultMap.put("so2HB", "-");
                resultMap.put("no2HB", "-");
                resultMap.put("coHB", "-");
                resultMap.put("o3HB", "-");
            }
            boolean tbFlag = true;
            // 上年的周数据
            for (SiteAirQualityPO lastYearMap : lastYearWeekList) {
                String lastSiteCode = lastYearMap.getSITECODE();
                if (lastSiteCode.equals(curSiteCode)) {
                    tbFlag = false;
                    String lastAQI = lastYearMap.getAQI();
                    String lastCV = lastYearMap.getCOMPOSITEVALUE();
                    String lastPM10 = lastYearMap.getPM10().getCONCENTRATION();
                    String lastPM25 = lastYearMap.getPM25().getCONCENTRATION();
                    String lastSO2 = lastYearMap.getSO2().getCONCENTRATION();
                    String lastNO2 = lastYearMap.getNO2().getCONCENTRATION();
                    String lastCO = lastYearMap.getCO().getCONCENTRATION();
                    String lastO3 = lastYearMap.getO3().getCONCENTRATION();
                    // 计算aqi的同比
                    if (null == curAQI || null == lastAQI || "0".equals(lastAQI)) {
                        resultMap.put("aqiTB", "-");
                    }else {
                        double aqiRetio = (Double.valueOf(curAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI)
                                * 100;
                        resultMap.put("aqiTB", decimalformat_2_scale.format(aqiRetio));
                    }
                    // 计算综合指数的同比
                    if (null == curCV || null == lastCV || "0.00".equals(lastCV) || !"0".equals(lastCV)) {
                        resultMap.put("cvTB","-");
                    }else {
                        double CVRetio = (Double.valueOf(curCV) - Double.valueOf(lastCV)) / Double.valueOf(lastCV)
                                * 100;
                        resultMap.put("cvTB", decimalformat_2_scale.format(CVRetio));
                    }
                    // pm10的同比
                    if (null == curPM10 || null == lastPM10 || "0".equals(lastPM10)) {
                        resultMap.put("pm10TB","-");
                    }else {
                        double PM10Retio = (Double.valueOf(curPM10) - Double.valueOf(lastPM10))
                                / Double.valueOf(lastPM10) * 100;
                        resultMap.put("pm10TB", decimalformat_2_scale.format(PM10Retio));
                    }
                    // pm25的同比
                    if (null == curPM25 || null == lastPM25 || "0".equals(lastPM25)) {
                        resultMap.put("pm25TB","-");
                    }else {
                        double PM25Retio = (Double.valueOf(curAQI) - Double.valueOf(lastPM25))
                                / Double.valueOf(lastPM25) * 100;
                        resultMap.put("pm25TB", decimalformat_2_scale.format(PM25Retio));
                    }
                    // SO2的同比
                    if (null == curSO2 || null == lastSO2 || "0".equals(lastSO2)) {
                        resultMap.put("so2TB", "-");
                    }else {
                        double SO2Retio = (Double.valueOf(curSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2)
                                * 100;
                        resultMap.put("so2TB", decimalformat_2_scale.format(SO2Retio));
                    }
                    // NO2的同比
                    if (null == curNO2 || null == lastNO2 || "0".equals(lastNO2)) {
                        resultMap.put("no2TB", "-");
                    }else {
                        double NO2Retio = (Double.valueOf(curNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2)
                                * 100;
                        resultMap.put("no2TB", decimalformat_2_scale.format(NO2Retio));
                    }
                    // CO的同比
                    if (null == curCO || null == lastCO || "0.00".equals(lastCO) || "0".equals(lastCO)) {
                        resultMap.put("coTB", "-");
                    }else {
                        double CORetio = (Double.valueOf(curCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO)
                                * 100;
                        resultMap.put("coTB", decimalformat_2_scale.format(CORetio));
                    }
                    // O3的同比
                    if (null == curO3 || null == lastO3 || "0".equals(lastO3)) {
                        resultMap.put("o3TB", "-");
                    }else {
                        double O3Retio = (Double.valueOf(curO3) - Double.valueOf(lastO3)) / Integer.valueOf(lastO3)
                                * 100;
                        resultMap.put("o3TB", decimalformat_2_scale.format(O3Retio));
                    }
                    break;
                }
            }
            if (tbFlag) {
                resultMap.put("aqiTB", "-");
                resultMap.put("cvTB", "-");
                resultMap.put("pm10TB", "-");
                resultMap.put("pm25TB", "-");
                resultMap.put("so2TB", "-");
                resultMap.put("no2TB", "-");
                resultMap.put("coTB", "-");
                resultMap.put("o3TB", "-");
            }
            resultList.add(resultMap);
        }
        return resultList;
    }

    private List<Map<String, Object>> setAirQualityHBMethodByMonth
        (List<SiteAirQualityPO> curMonthList, List<SiteAirQualityPO> lastMonthList) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        // 计算周环比
        for (SiteAirQualityPO curMonthMap : curMonthList) {
            Map<String, Object> resultMap = new HashMap<>();
            // 点位编码
            String curSiteCode = curMonthMap.getSITECODE();
            String curAQI = curMonthMap.getAQI();
            String curCV = curMonthMap.getCOMPOSITEVALUE();
            String curPM10 = curMonthMap.getPM10().getCONCENTRATION();
            String curPM25 = curMonthMap.getPM25().getCONCENTRATION();
            String curSO2 = curMonthMap.getSO2().getCONCENTRATION();
            String curNO2 = curMonthMap.getNO2().getCONCENTRATION();
            String curCO = curMonthMap.getCO().getCONCENTRATION();
            String curO3 = curMonthMap.getO3().getCONCENTRATION();
            resultMap.put("SITECODE", curSiteCode);
            resultMap.put("COMPOSITEVALUE", curCV);
            resultMap.put("PM10", curPM10);
            resultMap.put("PM25", curPM25);
            resultMap.put("SO2", curSO2);
            resultMap.put("NO2", curNO2);
            resultMap.put("CO", curCO);
            resultMap.put("O3_8", curO3);
            boolean hbFlag = true;
            // 上周的数据
            for (SiteAirQualityPO lastWeekPO : lastMonthList) {
                String lastSiteCode = lastWeekPO.getSITECODE();
                if (lastSiteCode.equals(curSiteCode)) {
                    hbFlag = false;
                    String lastAQI = lastWeekPO.getAQI();
                    String lastCV = lastWeekPO.getCOMPOSITEVALUE();
                    String lastPM10 = lastWeekPO.getPM10().getCONCENTRATION();
                    String lastPM25 = lastWeekPO.getPM25().getCONCENTRATION();
                    String lastSO2 = lastWeekPO.getSO2().getCONCENTRATION();
                    String lastNO2 = lastWeekPO.getNO2().getCONCENTRATION();
                    String lastCO = lastWeekPO.getCO().getCONCENTRATION();
                    String lastO3 = lastWeekPO.getO3().getCONCENTRATION();
                    // 计算aqi的环比值
                    if (null == curAQI || null == lastAQI || "0".equals(lastAQI)) {

                        resultMap.put("aqiHB", "-");
                    }else {
                        double aqiRetio = (Double.valueOf(curAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI)
                                * 100;
                        resultMap.put("aqiHB", decimalformat_2_scale.format(aqiRetio));
                    }
                    // 计算综合指数的环比值
                    if (null == curCV || null == lastCV || "0.00".equals(lastCV) || "0".equals(lastCV)) {

                        resultMap.put("cvHB", "-");
                    }else {
                        double CVRetio = (Double.valueOf(curCV) - Double.valueOf(lastCV)) / Double.valueOf(lastCV)
                                * 100;
                        resultMap.put("cvHB", decimalformat_2_scale.format(CVRetio));
                    }
                    // pm10的环比值
                    if (null == curPM10 || null == lastPM10 || "0".equals(lastPM10)) {

                        resultMap.put("pm10HB", "-");
                    }else {
                        double PM10Retio = (Double.valueOf(curPM10) - Double.valueOf(lastPM10))
                                / Double.valueOf(lastPM10) * 100;
                        resultMap.put("pm10HB", decimalformat_2_scale.format(PM10Retio));
                    }
                    // pm25的环比值
                    if (null == curPM25 || null == lastPM25 || "0".equals(lastPM25)) {

                        resultMap.put("pm25HB", "-");
                    }else {
                        double PM25Retio = (Double.valueOf(curAQI) - Double.valueOf(lastPM25))
                                / Double.valueOf(lastPM25) * 100;
                        resultMap.put("pm25HB", decimalformat_2_scale.format(PM25Retio));
                    }
                    // SO2的环比值
                    if (null == curSO2 || null == lastSO2 || "0".equals(lastSO2)) {

                        resultMap.put("so2HB", "-");
                    }else {
                        double SO2Retio = (Double.valueOf(curSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2)
                                * 100;
                        resultMap.put("so2HB", decimalformat_2_scale.format(SO2Retio));
                    }
                    // NO2的环比值
                    if (null == curNO2 || null == lastNO2 || "0".equals(lastNO2)) {

                        resultMap.put("no2HB", "-");
                    }else {
                        double NO2Retio = (Double.valueOf(curNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2)
                                * 100;
                        resultMap.put("no2HB", decimalformat_2_scale.format(NO2Retio));
                    }
                    // CO的环比值
                    if (null == curCO || null == lastCO || "0.00".equals(lastCO) || "0".equals(lastCO)) {

                        resultMap.put("coHB", "-");
                    }else {
                        double CORetio = (Double.valueOf(curCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO)
                                * 100;
                        resultMap.put("coHB", decimalformat_2_scale.format(CORetio));
                    }
                    // O3的环比值
                    if (null == curO3 || null == lastO3 || "0".equals(lastO3)) {
                        resultMap.put("o3HB", "-");

                    }else {
                        double O3Retio = (Double.valueOf(curO3) - Double.valueOf(lastO3)) / Double.valueOf(lastO3)
                                * 100;
                        resultMap.put("o3HB", decimalformat_2_scale.format(O3Retio));
                    }
                    break;
                }
            }
            if (hbFlag) {
                resultMap.put("aqiHB", "-");
                resultMap.put("cvHB", "-");
                resultMap.put("pm10HB", "-");
                resultMap.put("pm25HB", "-");
                resultMap.put("so2HB", "-");
                resultMap.put("no2HB", "-");
                resultMap.put("coHB", "-");
                resultMap.put("o3HB", "-");
            }
            resultList.add(resultMap);
        }
        return resultList;
    }

    private List<Map<String, Object>> setAirQualityTBAndHBMethodByMonth(List<SiteAirQualityPO> curMonthList,
                                                                        List<SiteAirQualityPO> lastYearList,
                                                                        List<SiteAirQualityPO> lastMonthList) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        // 计算周同比
        for (SiteAirQualityPO curMonthMap : curMonthList) {
            Map<String, Object> resultMap = new HashMap<>();
            // 点位编码
            String curSiteCode = curMonthMap.getSITECODE();
            String curAQI = curMonthMap.getAQI();
            String curCV = curMonthMap.getCOMPOSITEVALUE();
            String curPM10 = curMonthMap.getPM10().getCONCENTRATION();
            String curPM25 = curMonthMap.getPM25().getCONCENTRATION();
            String curSO2 = curMonthMap.getSO2().getCONCENTRATION();
            String curNO2 = curMonthMap.getNO2().getCONCENTRATION();
            String curCO = curMonthMap.getCO().getCONCENTRATION();
            String curO3 = curMonthMap.getO3().getCONCENTRATION();
            resultMap.put("SITECODE", curSiteCode);
            resultMap.put("COMPOSITEVALUE", curCV);
            resultMap.put("PM10", curPM10);
            resultMap.put("PM25", curPM25);
            resultMap.put("SO2", curSO2);
            resultMap.put("NO2", curNO2);
            resultMap.put("CO", curCO);
            resultMap.put("O3_8", curO3);
            boolean tbFlag = true;
            // 上年的周数据
            for (SiteAirQualityPO lastYearMap : lastYearList) {
                String lastSiteCode = lastYearMap.getSITECODE();
                if (lastSiteCode.equals(curSiteCode)) {
                    tbFlag = false;
                    String lastAQI = lastYearMap.getAQI();
                    String lastCV = lastYearMap.getCOMPOSITEVALUE();
                    String lastPM10 = lastYearMap.getPM10().getCONCENTRATION();
                    String lastPM25 = lastYearMap.getPM25().getCONCENTRATION();
                    String lastSO2 = lastYearMap.getSO2().getCONCENTRATION();
                    String lastNO2 = lastYearMap.getNO2().getCONCENTRATION();
                    String lastCO = lastYearMap.getCO().getCONCENTRATION();
                    String lastO3 = lastYearMap.getO3().getCONCENTRATION();
                    // 计算aqi的同比
                    if (null == curAQI || null == lastAQI || "0".equals(lastAQI)) {
                        resultMap.put("aqiTB", "-");
                    }else {
                        double aqiRetio = (Double.valueOf(curAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI)
                                * 100;
                        resultMap.put("aqiTB", decimalformat_2_scale.format(aqiRetio));
                    }
                    // 计算综合指数的同比
                    if (null == curCV || null == lastCV || "0.00".equals(lastCV) || !"0".equals(lastCV)) {
                        resultMap.put("cvTB","-");
                    }else {
                        double CVRetio = (Double.valueOf(curCV) - Double.valueOf(lastCV)) / Double.valueOf(lastCV)
                                * 100;
                        resultMap.put("cvTB", decimalformat_2_scale.format(CVRetio));
                    }
                    // pm10的同比
                    if (null == curPM10 || null == lastPM10 || "0".equals(lastPM10)) {
                        resultMap.put("pm10TB","-");
                    }else {
                        double PM10Retio = (Double.valueOf(curPM10) - Double.valueOf(lastPM10))
                                / Double.valueOf(lastPM10) * 100;
                        resultMap.put("pm10TB", decimalformat_2_scale.format(PM10Retio));
                    }
                    // pm25的同比
                    if (null == curPM25 || null == lastPM25 || "0".equals(lastPM25)) {
                        resultMap.put("pm25TB","-");
                    }else {
                        double PM25Retio = (Double.valueOf(curAQI) - Double.valueOf(lastPM25))
                                / Double.valueOf(lastPM25) * 100;
                        resultMap.put("pm25TB", decimalformat_2_scale.format(PM25Retio));
                    }
                    // SO2的同比
                    if (null == curSO2 || null == lastSO2 || "0".equals(lastSO2)) {
                        resultMap.put("so2TB", "-");
                    }else {
                        double SO2Retio = (Double.valueOf(curSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2)
                                * 100;
                        resultMap.put("so2TB", decimalformat_2_scale.format(SO2Retio));
                    }
                    // NO2的同比
                    if (null == curNO2 || null == lastNO2 || "0".equals(lastNO2)) {
                        resultMap.put("no2TB", "-");
                    }else {
                        double NO2Retio = (Double.valueOf(curNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2)
                                * 100;
                        resultMap.put("no2TB", decimalformat_2_scale.format(NO2Retio));
                    }
                    // CO的同比
                    if (null == curCO || null == lastCO || "0.00".equals(lastCO) || "0".equals(lastCO)) {
                        resultMap.put("coTB", "-");
                    }else {
                        double CORetio = (Double.valueOf(curCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO)
                                * 100;
                        resultMap.put("coTB", decimalformat_2_scale.format(CORetio));
                    }
                    // O3的同比
                    if (null == curO3 || null == lastO3 || "0".equals(lastO3)) {
                        resultMap.put("o3TB", "-");
                    }else {
                        double O3Retio = (Double.valueOf(curO3) - Double.valueOf(lastO3)) / Integer.valueOf(lastO3)
                                * 100;
                        resultMap.put("o3TB", decimalformat_2_scale.format(O3Retio));
                    }
                    break;
                }
            }
            if (tbFlag) {
                resultMap.put("aqiTB", "-");
                resultMap.put("cvTB", "-");
                resultMap.put("pm10TB", "-");
                resultMap.put("pm25TB", "-");
                resultMap.put("so2TB", "-");
                resultMap.put("no2TB", "-");
                resultMap.put("coTB", "-");
                resultMap.put("o3TB", "-");
            }
            boolean hbFlag = true;
            // 上周的数据
            for (SiteAirQualityPO lastWeekPO : lastMonthList) {
                String lastSiteCode = lastWeekPO.getSITECODE();
                if (lastSiteCode.equals(curSiteCode)) {
                    hbFlag = false;
                    String lastAQI = lastWeekPO.getAQI();
                    String lastCV = lastWeekPO.getCOMPOSITEVALUE();
                    String lastPM10 = lastWeekPO.getPM10().getCONCENTRATION();
                    String lastPM25 = lastWeekPO.getPM25().getCONCENTRATION();
                    String lastSO2 = lastWeekPO.getSO2().getCONCENTRATION();
                    String lastNO2 = lastWeekPO.getNO2().getCONCENTRATION();
                    String lastCO = lastWeekPO.getCO().getCONCENTRATION();
                    String lastO3 = lastWeekPO.getO3().getCONCENTRATION();
                    // 计算aqi的环比值
                    if (null == curAQI || null == lastAQI || "0".equals(lastAQI)) {

                        resultMap.put("aqiHB", "-");
                    }else {
                        double aqiRetio = (Double.valueOf(curAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI)
                                * 100;
                        resultMap.put("aqiHB", decimalformat_2_scale.format(aqiRetio));
                    }
                    // 计算综合指数的环比值
                    if (null == curCV || null == lastCV || "0.00".equals(lastCV) || "0".equals(lastCV)) {

                        resultMap.put("cvHB", "-");
                    }else {
                        double CVRetio = (Double.valueOf(curCV) - Double.valueOf(lastCV)) / Double.valueOf(lastCV)
                                * 100;
                        resultMap.put("cvHB", decimalformat_2_scale.format(CVRetio));
                    }
                    // pm10的环比值
                    if (null == curPM10 || null == lastPM10 || "0".equals(lastPM10)) {

                        resultMap.put("pm10HB", "-");
                    }else {
                        double PM10Retio = (Double.valueOf(curPM10) - Double.valueOf(lastPM10))
                                / Double.valueOf(lastPM10) * 100;
                        resultMap.put("pm10HB", decimalformat_2_scale.format(PM10Retio));
                    }
                    // pm25的环比值
                    if (null == curPM25 || null == lastPM25 || "0".equals(lastPM25)) {

                        resultMap.put("pm25HB", "-");
                    }else {
                        double PM25Retio = (Double.valueOf(curAQI) - Double.valueOf(lastPM25))
                                / Double.valueOf(lastPM25) * 100;
                        resultMap.put("pm25HB", decimalformat_2_scale.format(PM25Retio));
                    }
                    // SO2的环比值
                    if (null == curSO2 || null == lastSO2 || "0".equals(lastSO2)) {

                        resultMap.put("so2HB", "-");
                    }else {
                        double SO2Retio = (Double.valueOf(curSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2)
                                * 100;
                        resultMap.put("so2HB", decimalformat_2_scale.format(SO2Retio));
                    }
                    // NO2的环比值
                    if (null == curNO2 || null == lastNO2 || "0".equals(lastNO2)) {

                        resultMap.put("no2HB", "-");
                    }else {
                        double NO2Retio = (Double.valueOf(curNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2)
                                * 100;
                        resultMap.put("no2HB", decimalformat_2_scale.format(NO2Retio));
                    }
                    // CO的环比值
                    if (null == curCO || null == lastCO || "0.00".equals(lastCO) || "0".equals(lastCO)) {

                        resultMap.put("coHB", "-");
                    }else {
                        double CORetio = (Double.valueOf(curCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO)
                                * 100;
                        resultMap.put("coHB", decimalformat_2_scale.format(CORetio));
                    }
                    // O3的环比值
                    if (null == curO3 || null == lastO3 || "0".equals(lastO3)) {
                        resultMap.put("o3HB", "-");

                    }else {
                        double O3Retio = (Double.valueOf(curO3) - Double.valueOf(lastO3)) / Double.valueOf(lastO3)
                                * 100;
                        resultMap.put("o3HB", decimalformat_2_scale.format(O3Retio));
                    }
                    break;
                }
            }
            if (hbFlag) {
                resultMap.put("aqiHB", "-");
                resultMap.put("cvHB", "-");
                resultMap.put("pm10HB", "-");
                resultMap.put("pm25HB", "-");
                resultMap.put("so2HB", "-");
                resultMap.put("no2HB", "-");
                resultMap.put("coHB", "-");
                resultMap.put("o3HB", "-");
            }
            resultList.add(resultMap);
        }
        return resultList;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    private void setSiteNameMetiod
        (List<Map<String, Object>> siteInfoList, List<Map<String, Object>> curWeekList) {
        // 排名
        int index = 1;
        /**
         * 设置点位名称、同比值、环比值
         */
        for (Map weekMap : curWeekList) {
            String weekSiteCode = weekMap.get("SITECODE").toString();
            weekMap.put("index", index++);
            // 设置点位名称
            for (Map<String, Object> siteMap : siteInfoList) {
                String siteCode = String.valueOf(siteMap.get("siteCode"));
                if (weekSiteCode.equals(siteCode)) {
                    weekMap.put("siteName", siteMap.get("siteName"));
                    weekMap.put("regionName", siteMap.get("RegionName"));
                    break;
                }
            }
        }
    }

    @Override
    public MsgResult findAirQualityContrastThrend(Map<String, Object> paraMap) {
        // 选中的点位编码
        String checkedSiteCodes = paraMap.get("checkedSiteCodes").toString();
        // checkedSiteCodes = "410100004,410100051,410100052";

        // 数据类型 时报hourData；日报dayData
        String dataType = paraMap.get("dataType").toString();

        // 空气质量因子:AQI,综合指数 compositeValue,PM10,PM25,SO2,NO2,CO,O3
        String qualityFactor = paraMap.get("qualityFactor").toString();

        // 开始时间
        String startTime = paraMap.get("startTime").toString();
        // startTime = "2017-02-15 15:00:00";
        // 结束时间
        String endTime = paraMap.get("endTime").toString();
        // endTime = "2017-02-16 12:00:00";

        // 选择的点位编码字符串转换为数组
        String[] checkedSiteCodeArr = checkedSiteCodes.split(",");
        // 选中点位编码数组转换为列表
        List<String> checkedSiteCodeList = Arrays.asList(checkedSiteCodeArr);

        // 选择点位 的点位信息
        List<Map<String, Object>> checkedSiteInfoList = regionSiteDao.findSiteInfoBySiteCode(checkedSiteCodeList);
        if (null == checkedSiteInfoList || checkedSiteInfoList.size() == 0) {
            return MsgResult.errorResult("没有查询到选中点位的信息，查询失败。");
        }

        // 查询Mongo的查询条件
        SiteAirParam siteAirParam = new SiteAirParam();
        // 要查询的点位列表
        siteAirParam.setSiteCodes(checkedSiteCodeList);
        siteAirParam.setStartTime(startTime);
        siteAirParam.setEndTime(endTime);

        /**
         * 因为国控站和小型站数据格式不同，需要把格式统一
         */
        List<Map<String, Object>> originalDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> originalMap = null;
        //echart封装数据时的时间点数组
        List<String> timeDetailList = new ArrayList<>();
        // 时报
        if (dataType.equals(DataTypeEnum.HOUR_DATA.getValue())) {
            //根据开始时间和结束时间以1小时为间隔，得到时间点数组
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 16), endTime.substring(0, 16), "hour");
            // 查询小时空气质量数据
            List<SiteAirQualityPO> hourQualityMapList = siteAirDao.findSiteAirHourDataBySiteCodesAndTime(siteAirParam);
            if (null != hourQualityMapList) {
                for (SiteAirQualityPO hourMap : hourQualityMapList) {
                    originalMap = new HashMap<String, Object>();
                    originalMap.put("SITECODE", hourMap.getSITECODE());
                    originalMap.put("MONITORTIME", hourMap.getMONITORTIME());
                    originalMap.put("AQI", hourMap.getAQI());
                    originalMap.put("COMPOSITEVALUE", hourMap.getCOMPOSITEVALUE());
                    originalMap.put("PM10", hourMap.getPM10().getCONCENTRATION());
                    originalMap.put("PM25", hourMap.getPM25().getCONCENTRATION());
                    originalMap.put("SO2", hourMap.getSO2().getCONCENTRATION());
                    originalMap.put("NO2", hourMap.getNO2().getCONCENTRATION());
                    originalMap.put("CO", hourMap.getCO().getCONCENTRATION());
                    originalMap.put("O3", hourMap.getO3().getCONCENTRATION());

                    originalDataList.add(originalMap);
                }
            }

            // 日报
        } else if (dataType.equals(DataTypeEnum.DAY_DATA.getValue())) {
            // 根据开始时间和结束时间以1天为间隔，得到时间点数组
            timeDetailList = CalendarUtil.getDatesBetweenTwoDate(startTime.substring(0, 10), endTime.substring(0, 10), "day");
            // 查询日报空气质量数据
            List<SiteAirQualityPO> dayQualityList = siteAirDao.findSiteAirDayDataBySiteCodesAndTime(siteAirParam);
            if (null != dayQualityList) {
                for (SiteAirQualityPO dayMap : dayQualityList) {
                    originalMap = new HashMap<String, Object>();
                    originalMap.put("SITECODE", dayMap.getSITECODE());
                    originalMap.put("MONITORTIME", dayMap.getMONITORTIME());
                    originalMap.put("AQI", dayMap.getAQI());
                    originalMap.put("COMPOSITEVALUE", dayMap.getCOMPOSITEVALUE());
                    originalMap.put("PM10", dayMap.getPM10().getCONCENTRATION());
                    originalMap.put("PM25", dayMap.getPM25().getCONCENTRATION());
                    originalMap.put("SO2", dayMap.getSO2().getCONCENTRATION());
                    originalMap.put("NO2", dayMap.getNO2().getCONCENTRATION());
                    originalMap.put("CO", dayMap.getCO().getCONCENTRATION());
                    originalMap.put("O3", dayMap.getO3().getCONCENTRATION());

                    originalDataList.add(originalMap);
                }
            }

        }

        if (originalDataList.size() == 0) {
            return MsgResult.errorResult("选中的点位没有小时数据");
        }

        // 把数据按照时间，正序排列，方便eCharts显示
        Collections.reverse(originalDataList);

        // 组装eCharts数据
        List<String> siteNameList = new ArrayList<String>();
        List<List<String>> timeList = new ArrayList<List<String>>();
        List<List<String>> dataList = new ArrayList<List<String>>();
        List<String> siteDateList = new ArrayList<>();
        boolean timeExist = false;
        // 把整理好的列表，整理成eCharts的列表
        for (Map<String, Object> checkedSiteMap : checkedSiteInfoList) {
            String checkedSiteCode = String.valueOf(checkedSiteMap.get("siteCode"));
            String checkedSiteName = String.valueOf(checkedSiteMap.get("siteName"));
            // 提取点位名字
            siteNameList.add(checkedSiteName);
            siteDateList = new ArrayList<String>();
            //遍历时间点数组，如果originalData的MONITORTIME等于时间点，则填充时间；否则填充"-"
            for (String detailTime : timeDetailList) {
                timeExist = false;
                for (Map<String, Object> hourMap : originalDataList) {

                    String siteCode = hourMap.get("SITECODE").toString();
                    if (siteCode.equals(checkedSiteCode)) {
                        String time = hourMap.get("MONITORTIME").toString();
                        if (null == time || "".equals(time)) {
                            continue;
                        }
                        time = time.substring(0, 16);
                        if (dataType.equals(DataTypeEnum.DAY_DATA.getValue())) {
                            time = time.substring(0, 10);
                        }
                        // 提取时间
                        if (time.equals(detailTime)) {
                            switch (qualityFactor) {
                                case "AQI": {
                                    Object aqi = hourMap.get("AQI");
                                    aqi = null == aqi || "".equals(aqi) ? "-" : aqi;
                                    // 提取aqi
                                    siteDateList.add(aqi.toString());
                                    break;
                                }
                                case "compositeValue": {
                                    Object aqi = hourMap.get("COMPOSITEVALUE");
                                    aqi = null == aqi || "".equals(aqi) ? "-" : aqi;
                                    // 提取aqi
                                    siteDateList.add(aqi.toString());
                                    break;
                                }
                                case "PM10":
                                    Object pm10 = hourMap.get("PM10");
                                    pm10 = null == pm10 || "".equals(pm10) ? "-" : pm10;
                                    // 提取pm10
                                    siteDateList.add(pm10.toString());
                                    break;
                                case "PM25":
                                    Object pm25 = hourMap.get("PM25");
                                    pm25 = null == pm25 || "".equals(pm25) ? "-" : pm25;
                                    // 提取pm25
                                    siteDateList.add(pm25.toString());
                                    break;
                                case "SO2":
                                    Object so2 = hourMap.get("SO2");
                                    so2 = null == so2 || "".equals(so2) ? "-" : so2;
                                    // 提取so2
                                    siteDateList.add(so2.toString());
                                    break;
                                case "NO2":
                                    Object no2 = hourMap.get("NO2");
                                    no2 = null == no2 || "".equals(no2) ? "-" : no2;
                                    // 提取no2
                                    siteDateList.add(no2.toString());
                                    break;
                                case "CO":
                                    Object co = hourMap.get("CO");
                                    co = null == co || "".equals(co) ? "-" : co;
                                    // 提取co
                                    siteDateList.add(co.toString());
                                    break;
                                case "O3":
                                    Object o3 = hourMap.get("O3");
                                    o3 = null == o3 || "".equals(o3) ? "-" : o3;
                                    // 提取o3
                                    siteDateList.add(o3.toString());
                                    break;
                            }
                            timeExist = true;
                            break;
                        }
                    }
                }
                if (!timeExist) {
                    siteDateList.add("-");
                }
            }
            timeList.add(timeDetailList);
            dataList.add(siteDateList);

        }  // end for}

        JSONObject resultObj = new JSONObject();
        resultObj.put("siteNameList", siteNameList);
        resultObj.put("timeList", timeList);
        resultObj.put("dataList", dataList);
        return MsgResult.okResult(resultObj);
    }

    @Override
    public List<Map<String, Object>> findGPointNewMinuteDataBySiteCode(List<String> siteCodeList) {
        // 查询上个小时的数据
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        SimpleDateFormat sdf2 = new SimpleDateFormat(DamConstant.ALL_SDF_YMD_H);
        String lastHour = sdf2.format(calendar.getTime());
        SiteAirParam siteAirParam = new SiteAirParam();
        siteAirParam.setSiteCodes(siteCodeList);
        siteAirParam.setStartTime(lastHour);
        siteAirParam.setEndTime(lastHour);
        List<SiteAirQualityPO> gpointNewDataList = siteAirDao.findSiteAirHourNewDataBySiteCodes(siteAirParam);
        if (null == gpointNewDataList || gpointNewDataList.size() < 1) {
            gpointNewDataList = siteAirDao.findSiteAirHourNewDataBySiteCodes(siteAirParam);
        }
        List<Map<String, Object>> aqiList = new ArrayList<Map<String, Object>>();
        dealGPointAqiData(gpointNewDataList, aqiList);
        return aqiList;
    }


    @Override
    public List<Map<String, Object>> findGPointNewMinuteDataBySiteCodeList(List<String> siteCodeList) {
        // 先查询30分钟前的5分钟数据
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf2 = new SimpleDateFormat(DamConstant.SDF_YMD_HMS);
        // 当前时间作为结束时间
        String endTime = sdf2.format(calendar.getTime());
        calendar.add(Calendar.MINUTE, -30);
        // 十分钟前的数据作为开始时间
        String startTime = sdf2.format(calendar.getTime());
        SiteAirParam siteAirParam = new SiteAirParam();
        siteAirParam.setStartTime(startTime);
        siteAirParam.setEndTime(endTime);
        siteAirParam.setSiteCodes(siteCodeList);
        List<SiteAirQualityPO> gpointNewDataList = siteAirDao.findSiteAirMinDataBySiteCodesAndTime(siteAirParam);
        if (null == gpointNewDataList || gpointNewDataList.size() < 1) {
            // 如果为空,查询上个小时的小时数据
            SimpleDateFormat sdfH = new SimpleDateFormat(DamConstant.ALL_SDF_YMD_H);
            calendar.setTime(new Date());
            String lastHour = sdfH.format(calendar.getTime());
            siteAirParam.setStartTime(lastHour);
            siteAirParam.setEndTime(lastHour);
            gpointNewDataList = siteAirDao.findSiteAirHourDataBySiteCodesAndTime(siteAirParam);
            if (null == gpointNewDataList || gpointNewDataList.size() < 1) {
                // 如果为空,查询最新的小时数据
                gpointNewDataList = siteAirDao.findSiteAirHourNewDataBySiteCodes(siteAirParam);
            }
        }

        List<Map<String, Object>> aqiList = new ArrayList<Map<String, Object>>();

        dealGPointAqiData(gpointNewDataList, aqiList);
        return aqiList;
    }

    @Override
    public List<Map<String, Object>> findGPointNewHourDataBySiteCodes(List<String> siteCodeList) {
        SiteAirParam siteAirParam = new SiteAirParam();
        siteAirParam.setSiteCodes(siteCodeList);
        List<SiteAirQualityPO> gpointNewDataList = siteAirDao.findSiteAirHourNewDataBySiteCodes(siteAirParam);
        List<Map<String, Object>> aqiList = new ArrayList<Map<String, Object>>();
        dealGPointAqiData(gpointNewDataList, aqiList);
        return aqiList;
    }

    @Override
    public List<Map<String, Object>> findLast12HourGSiteHourDataBySiteCode(String siteCode) {
        SiteAirParam siteAirParam = new SiteAirParam();
        siteAirParam.setSiteCode(siteCode);
        List<SiteAirQualityPO> gpointHourList = siteAirDao.findSiteAirHour12DataBySiteCode(siteAirParam);

        List<Map<String, Object>> aqiList = new ArrayList<Map<String, Object>>();
        dealGPointAqiData(gpointHourList, aqiList);

        return aqiList;
    }


    private void dealGPointAqiData(List<SiteAirQualityPO> gpointNewDataList, List<Map<String, Object>> aqiList) {
        Map<String, Object> aqiMap = null;
        for (SiteAirQualityPO hourAirMap : gpointNewDataList) {
            aqiMap = new HashMap<String, Object>();
            aqiMap.put("aqi", hourAirMap.getAQI() == null ? "0" : hourAirMap.getAQI());
            aqiMap.put("aqiLevel", hourAirMap.getAQILEVEL());
            aqiMap.put("aqiLevelType", hourAirMap.getAQILEVELTYPE());
            aqiMap.put("pollutant", hourAirMap.getPOLLUTANT());
            aqiMap.put("compositeValue", hourAirMap.getCOMPOSITEVALUE() == null ? "0" : hourAirMap.getCOMPOSITEVALUE());

            aqiMap.put("pm10", hourAirMap.getPM10().getCONCENTRATION());
            aqiMap.put("pm10IAQI", hourAirMap.getPM10().getIAQI());

            aqiMap.put("pm25", hourAirMap.getPM25().getCONCENTRATION());
            aqiMap.put("pm25IAQI", hourAirMap.getPM25().getIAQI());

            aqiMap.put("so2", hourAirMap.getSO2().getCONCENTRATION());
            aqiMap.put("so2IAQI", hourAirMap.getSO2().getIAQI());

            aqiMap.put("no2", hourAirMap.getNO2().getCONCENTRATION());
            aqiMap.put("no2IAQI", hourAirMap.getNO2().getIAQI());

            aqiMap.put("co", hourAirMap.getCO().getCONCENTRATION());
            aqiMap.put("coIAQI", hourAirMap.getCO().getIAQI());

            aqiMap.put("o3", hourAirMap.getO3().getCONCENTRATION());
            aqiMap.put("o3IAQI", hourAirMap.getO3().getIAQI());

            aqiMap.put("siteCode", hourAirMap.getSITECODE());
            aqiMap.put("monitorTime", hourAirMap.getMONITORTIME());

            aqiMap.put("temp", hourAirMap.getTEMP() == null ? "" : hourAirMap.getTEMP());
            aqiMap.put("hum", hourAirMap.getHUM() == null ? "" : hourAirMap.getHUM());
            aqiMap.put("windDirection", hourAirMap.getWD() == null ? "" : WeatherUtil.changeAngleToWindDirectionWithEng(String.valueOf(hourAirMap.getWD())));
            aqiMap.put("windSpeed", hourAirMap.getWS() == null ? "" : WeatherUtil.changeWindSpeedToPowerNoCN(String.valueOf(hourAirMap.getWS())));

            aqiList.add(aqiMap);
        }

    }

}
